package com.wman.drawing.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.wman.sheep.common.utils.DensityUtil;

import androidx.annotation.Nullable;

/**
 * @ProjectName: wman
 * @Package: com.wman.drawing.view
 * @ClassName: SportsView
 * @Description: java类作用描述
 * @Author: wman
 * @CreateDate: 2019/11/6 9:42
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/11/6 9:42
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class SportsView extends View {
    private final static int RING_WIDTH = DensityUtil.dip2px(20);
    private final static int RADIUS = DensityUtil.dip2px(150);
    private final static int CIRCLE_COLOR = Color.parseColor("#90A4AE");
    private final static int HIGHTLIGHT_COLOR = Color.parseColor("#FF4081");

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Rect mRect = new Rect();
    private Paint.FontMetrics mFontMetrics;

    public SportsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    {
        mPaint.setTextSize(DensityUtil.dip2px(80));
        mPaint.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/xiyuan.TTF"));
        mPaint.setTextAlign(Paint.Align.CENTER);
        //居中算法2，顶线和底线
        mFontMetrics = mPaint.getFontMetrics();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(CIRCLE_COLOR);
        mPaint.setStrokeWidth(RING_WIDTH);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, RADIUS, mPaint);

        mPaint.setColor(HIGHTLIGHT_COLOR);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawArc(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS,
                getWidth() / 2 + RADIUS, getHeight() / 2 + RADIUS, -90, 225, false, mPaint);

        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(DensityUtil.dip2px(80));
        mPaint.setTextAlign(Paint.Align.CENTER);
        //居中算法1，非常的居中  变化的内容可能会跳动
        mPaint.getTextBounds("abab", 0, "abab".length(), mRect);
//        int offset = (mRect.top + mRect.bottom) / 2;
        int offset = (int) ((mFontMetrics.ascent + mFontMetrics.descent) / 2);
        canvas.drawText("abab", getWidth() / 2, getHeight() / 2 - offset, mPaint);

        mPaint.setTextAlign(Paint.Align.LEFT);
        //左边距的问题
        mPaint.setTextSize(DensityUtil.dip2px(150));
        mPaint.getTextBounds("abab", 0, "abab".length(), mRect);
        canvas.drawText("abab", -mRect.left, RADIUS, mPaint);

        //左边距的问题
        mPaint.setTextSize(DensityUtil.dip2px(15));
        mPaint.getTextBounds("abab", 0, "abab".length(), mRect);
        canvas.drawText("abab", -mRect.left, RADIUS + mPaint.getFontSpacing(), mPaint);
    }
}
